package com.sskj.video.my;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Like
 * @Date: 2024/3/4 15:23
 * @Description:
 */
@Component
public class HikUtil {
    /**
     * 请根据技术支持提供的实际的平台IP/端口和API网关中的合作方信息更换static静态块中的三个参数.
     * [1 host]
     * host格式为IP：Port，如10.0.0.1:443
     * 当使用https协议调用接口时，IP是平台（nginx）IP，Port是https协议的端口；
     * 当使用http协议调用接口时，IP是artemis服务的IP，Port是artemis服务的端口（默认9016）。
     * [2 appKey和appSecret]
     * 请按照技术支持提供的合作方Key和合作方Secret修改
     * appKey：合作方Key
     * appSecret：合作方Secret
     * 调用前看清接口传入的是什么，是传入json就用doPostStringArtemis方法，是表单提交就用doPostFromArtemis方法
     */

    @Autowired
    private HikConfig hikConfig;

    private static ArtemisConfig config;

    /**
     * API网关的后端服务上下文为：/artemis
     */
    private static String ARTEMIS_PATH;
    //private static String ARTEMIS_PATH = "/pxy/hik/artemis";

    @PostConstruct
    private void initConfig() throws Exception {
        config = new ArtemisConfig();
        // 代理API网关nginx服务器ip端口
        config.setHost(hikConfig.getHost());
        // 秘钥appkey
        config.setAppKey(hikConfig.getKey());
        // 秘钥appSecret
        config.setAppSecret(hikConfig.getSecret());

        ARTEMIS_PATH = hikConfig.getArtemisPath();
    }

    /**
     * 调用POST请求类型接口，这里以获取组织列表为例
     * 接口实际url:https://ip:port/artemis/api/resource/v1/org/orgList
     *
     * @return
     */
    public static String callPostApiGetOrgList() throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/org/orgList";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        paramMap.put("pageNo", "1");
        paramMap.put("pageSize", "2");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }

    /**
     * 调用POST请求类型接口，这里以分页获取区域列表为例
     * 接口实际url：https://ip:port/artemis/api/api/resource/v1/regions
     *
     * @return
     */
    public static String callPostApiGetRegions() throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/regions";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        paramMap.put("pageNo", "1");
        paramMap.put("pageSize", "2");
        paramMap.put("treeCode", "0");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }


    /**
     * 调用POST请求类型接口，这里以获取监控点位
     * 接口实际url:https://ip:port/artemis/api/resource/v1/cameras
     *
     * @return
     */
    public static String callPostV1Cameras() throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/cameras";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        paramMap.put("pageNo", "1");
        paramMap.put("pageSize", "100");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }

    /**
     * 调用POST请求类型接口，获取单个监控点信息是指根据监控点唯一标识来获取指定的监控点信息
     * 接口实际url:https://ip:port/artemis/api/resource/v1/cameras/indexCode
     *
     * @return
     */
    public static String callPostV1CamerasDetails(String cameraIndexCode) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/cameras/indexCode";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        paramMap.put("cameraIndexCode", cameraIndexCode);
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }

    /**
     * 调用POST请求类型接口，这里以获取视频流
     * 接口实际url:https://ip:port/artemis/api/video/v2/cameras/previewURLs
     *
     * @return
     */
    public static String callPostPreviewURLs(String cameraIndexCode) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/video/v2/cameras/previewURLs";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        paramMap.put("cameraIndexCode", cameraIndexCode);
        paramMap.put("streamType", "0");
        paramMap.put("protocol", "rtsp");
        paramMap.put("streamform", "rtp");
        paramMap.put("expand", "transcode=0");
        paramMap.put("transmode", "1");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }

    /**
     * 调用POST请求类型接口，这里以获取视频流
     * 接口实际url:https://ip:port/artemis/api/resource/v1/org/orgList
     *
     * @return
     */
    public static String callPostPreviewURLs(String cameraIndexCode, String protocol) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/video/v2/cameras/previewURLs";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        paramMap.put("cameraIndexCode", cameraIndexCode);
        paramMap.put("streamType", "0");
        paramMap.put("protocol", protocol);
        paramMap.put("streamform", "rtp");
        paramMap.put("expand", "transcode=0");
        paramMap.put("transmode", "1");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }


    /**
     * 海康注册事件监听接口
     * 调用POST请求类型接口，该接口用于满足应用方按事件类型码订阅事件，同一个用户重复订阅相同的事件，接口内部逻辑自动去重，确保不重复投递事件。
     * 接口实际url:https://ip:port/api/eventService/v1/eventSubscriptionByEventTypes
     *
     * @return
     */
    public static String callPostEventSubscriptionByEventTypes(int[] eventTypes, String eventDest, Integer subType, int[] eventLvl) {
        try{
            final String getCamsApi = ARTEMIS_PATH + "/api/eventService/v1/eventSubscriptionByEventTypes";
            Map paramMap = new HashMap();// post请求Form表单参数
            paramMap.put("eventTypes", eventTypes);
            paramMap.put("eventDest", eventDest);
            paramMap.put("subType", subType);
            paramMap.put("eventLvl", eventLvl);
            String body = JSON.toJSON(paramMap).toString();
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("https://", getCamsApi);
                }
            };
            return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 调用POST请求类型接口，该接口用于应用方指定事件类型取消订阅。
     * 接口实际url:https://ip:port/api/eventService/v1/eventUnSubscriptionByEventTypes
     *
     * @return
     */
    public static String callPostEventUnSubscriptionByEventTypes(int[] eventTypes) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/eventService/v1/eventUnSubscriptionByEventTypes";
        Map paramMap = new HashMap();// post请求Form表单参数
        paramMap.put("eventTypes", eventTypes);
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }

    /**
     * 调用POST请求类型接口，应用方根据用户查询事件订阅详情
     * 接口实际url:https://ip:port/api/eventService/v1/eventSubscriptionView
     *
     * @return
     */
    public static String callPostEventSubscriptionView() throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/eventService/v1/eventSubscriptionView";
        Map paramMap = new HashMap();// post请求Form表单参数
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }

    public static void main(String[] args) throws Exception {

//		String result = callPostApiGetOrgList();
        String V1CamerasResult = callPostV1Cameras();
        JSONObject jsonObject01 = JSON.parseObject(V1CamerasResult);
        JSONObject jsonObject02 = JSON.parseObject(String.valueOf(jsonObject01.get("data")));
        JSONArray jsonArray = JSONArray.parseArray(String.valueOf(jsonObject02.get("list")));
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject03 = JSON.parseObject(String.valueOf(jsonArray.get(i)));
            String VechicleDataResult = callPostPreviewURLs(String.valueOf(jsonObject03.get("cameraIndexCode")));
            JSONObject jsonObject04 = JSON.parseObject(String.valueOf(VechicleDataResult));
            System.out.println(jsonObject03.get("cameraName"));
            System.out.println(jsonObject04.get("data"));
            System.out.println("-----------");
            String details = callPostV1CamerasDetails(String.valueOf(jsonObject03.get("cameraIndexCode")));
            JSONObject jsonObject05 = JSON.parseObject(String.valueOf(details));
            System.out.println(jsonObject05.get("cameraName"));
            System.out.println(jsonObject05.get("data"));
        }
    }

}
